<template>
	<view>
		<view class="navBar">
			<view class="left">
				<uni-icons type="left" size="24" @click="toBack"></uni-icons>
			</view>
			<view class="center">用户注册</view>
			<view class="right"></view>
		</view>
		<view class="headText">
			<view class="register-cn">注册</view>
			<view class="register-en">please register to your accounts</view>
		</view>
		<view class="registerBox">
			<view class="userInput">
				<view class="">用户名:</view>
				<input type="text" v-model="username" placeholder="请输入用户名" />
			</view>
			<view class="emailInput">
				<view class="">邮箱:</view>
				<input type="email" v-model="email" placeholder="请输入邮箱" />
			</view>
			<view class="passwordInput">
				<view class="">密码:</view>
				<input type="password" v-model="password" placeholder="请输入密码" />
			</view>
			<view class="passwordInput">
				<view class="">确认密码:</view>
				<input type="password" v-model="repeatPassword" placeholder="请输入密码" />
			</view>
			<button class="register-btn" type="primary" @click="register">注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: "",
				email: '',
				password: '',
				repeatPassword: ''
			}
		},
		methods: {
			toBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			register() {
				if (this.username !== ""
				&& this.email !== ""
				&& this.password !== "") {
					if (this.password === this.repeatPassword) {
						uni.request({
							url: 'http://localhost:8080/register',
							method: 'POST',
							header: {
								'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
							},
							data: {
								username:this.username,
								email:this.email,
								password:this.password,
							},
							success: res => {
								console.log(res.data)
								uni.showToast({
									icon:"none",
									title: res.data.data
								});
								if(res.data.code===200){
									setTimeout(
										()=>{
											uni.navigateTo({
												url: '/pages/login/login',
												success: res => {},
												fail: () => {},
												complete: () => {}
											});
										},1000);
								}
							},
							fail: () => {},
							complete: () => {}
						});

					} else {
						uni.showToast({
							icon: "error",
							title: '两次密码不一致'
						});
					}
				} else {
					uni.showToast({
						icon: "error",
						title: '用户名、邮箱与密码不能为空'
					});
				}
			}
		}
	}
</script>


<style lang="scss">
	.navBar {
		display: flex;
		height: 100rpx;
		background-color: white;
		align-items: center;
		padding: 0 30rpx;
		/* #ifdef MP-WEIXIN */
		padding-top: 100rpx;
		/* #endif */

		.left {
			flex: 1;
		}

		.center {
			flex: 1;
			color: #646566;
			text-align: center;
			font-size: 34rpx;
			font-weight: 700;
		}

		.right {
			flex: 1;
		}
	}

	.headText {
		margin: 140rpx 30rpx;

		.register-cn {
			margin-bottom: 30rpx;
			font-size: 52rpx;
			color: #646566;
			font-weight: 700;
		}

		.register-en {
			font-size: 38rpx;
			font-weight: 400;
			color: #83838b;
		}

	}

	.registerBox {
		margin: 0 50rpx;

		.userInput,
		.emailInput,
		.passwordInput {
			display: flex;
			align-items: center;
			padding: 30rpx 0;
			border-bottom: 1rpx solid #ccc;

			view {
				flex: 1;
			}

			input {
				flex: 3;
			}
		}

		.register-btn {
			margin-top: 100rpx;
			border-radius: 50rpx;
			border: 2rpx solid #ccc;
		}

		.login-btn {
			margin-top: 50rpx;
			border-radius: 50rpx;
		}
	}
</style>